#include "widget.h"
#include "ui_widget.h"
#include "QIcon"
//图片类的头文件
#include <QPixmap>
#include <QPalette>
//文件系统
#include <QDir>
#include <QFile>
#include <QFileInfo>

//对话框
#include <QMessageBox>
//时间函数
#include <time.h>

//打印调试
#include <QDebug>

//动画
#include <QPropertyAnimation>

Widget::Widget(QWidget *parent)
    : QWidget(parent),
      m_mode(ORDER_MODE),
      m_isShowFlag(false),

      ui(new Ui::Widget)
{

    ui->setupUi(this);
    //设置标题
    setWindowTitle("音乐播放器");
    //音乐播放器
    m_player = new QMediaPlayer(this);
    //固定窗口大小
    setFixedSize(1024,650);
    //设置背景
    setBackGround(":/image/back1.jpg");
    //初始化按钮
    initButtons();
    //加载音乐
    QString musicPath = "D:\\qtpro\\musicplay\\musicplay1\\月光-胡彦斌.mp3";
    m_player->setMedia(QUrl::fromLocalFile(musicPath));//加载本地文件
    //播放音乐
    //m_player->play();

    //加载音乐文件夹
    m_musicDir = "D:\\qtpro\\musicplay\\musicplay1\\";
    loadAppointMusic(m_musicDir);
    //随机数的种子
    srand(time(NULL));

}
//设置背景
void Widget::setBackGround(const QString & filename)
{
    //创建照片
    QPixmap pixmap(filename);
    //获取当前窗口的大小
    QSize windowSize = this->size();
    //将照片缩放到当前窗口的大小
    QPixmap scalePixmap = pixmap.scaled(windowSize,Qt::IgnoreAspectRatio,Qt::SmoothTransformation);
    //创建QPattle对象并设置背景照片-调色板
    QPalette palette = this->palette();
    palette.setBrush(QPalette::Background,QBrush(scalePixmap));
    //将调色板应用到窗口上
    this->setPalette(palette);


}
//设置按钮的样式
void Widget::setButtonStyle(QPushButton * button,const QString & filename)
{
    //设置按钮的大小
    button->setFixedSize(50,50);
    //图标
    button->setIcon(QIcon(filename));//图标
    //
    button->setIconSize(QSize(ui->prevBtn->width(),ui->prevBtn->height()));
    //设置按钮的背景为透明色
    button->setStyleSheet("background-color:transparent");

}
//初始化按钮
void Widget:: initButtons()
{
    setButtonStyle(ui->prevBtn,":/Icon/prev.png");
    setButtonStyle(ui->playBtn,":/Icon/play.png");
    setButtonStyle(ui->nextBtn,":/Icon/next.png");
    setButtonStyle(ui->modeBtn,":/Icon/order.png");
    setButtonStyle(ui->listBtn,":/Icon/list.png");


ui->musiclist->setStyleSheet("QListWidget{"
                             "border: none;"
                             "border-radius:20px;"
                             "background-color:rgba(255,255,255,0,7);}");
//初始化隐藏列表
ui->musiclist->hide();





    //信号与槽
    connect(ui->playBtn,&QPushButton::clicked,this,&Widget::handlePlaySlot);
    connect(ui->modeBtn,&QPushButton::clicked,this,&Widget::handleModeSlot);
    connect(ui->nextBtn,&QPushButton::clicked,this,&Widget::handleNextSlot);
    connect(ui->prevBtn,&QPushButton::clicked,this,&Widget::handlePrevSlot);
    connect(ui->listBtn,&QPushButton::clicked,this,&Widget::handleListSlot);
    //处理音乐位置的变化
    connect(m_player,&QMediaPlayer::positionChanged,this,&Widget::handlePositionSlot);
    //处理音乐的总时长
    connect(m_player,&QMediaPlayer::durationChanged,this,&Widget::handleDurtionSlot);

}
//处理音乐时长
void Widget:: handleDurtionSlot(int duration)
{
    ui->progressBar->setRange(0,duration);
}

//处理音乐进度
void Widget:: handlePositionSlot(int position)
{
    //音乐的总时长-转换成进度的里程
    ui->progressBar->setValue(position);
    //将毫秒转换成mm::ss
}

//显示动画
void Widget:: showAnimation(QWidget  * window)
{
      QPropertyAnimation animation(window,"pos");
      //动画的持续时间
      animation.setDuration(1000);
      //起始坐标
      animation.setStartValue(QPoint(this->width(),0));
      //结束坐标
      animation.setEndValue(QPoint(this->width()-ui->musiclist->width(),0));
      animation.start();

      QEventLoop loop;

      //信号与槽：等待动画结果
      connect(&animation,&QPropertyAnimation::finished,&loop,&QEventLoop::quit);
      loop.exec();
}

//隐藏动画
void Widget:: hideAnimation(QWidget  * window)
{

    QPropertyAnimation animation(window,"pos");
    //动画的持续时间
    animation.setDuration(1000);
    animation.setStartValue(QPoint(this->width()-ui->musiclist->width(),0));
    animation.setEndValue(QPoint(this->width(),0));
    animation.start();
    QEventLoop loop;

    //信号与槽：等待动画结果
    connect(&animation,&QPropertyAnimation::finished,&loop,&QEventLoop::quit);
    loop.exec();
}
//处理音乐列表的槽
void Widget::handleListSlot()
{
    if(m_isShowFlag == false)
    {
        ui->musiclist->show();
        //动画方式出现：渐入效果
        showAnimation(ui->musiclist);
        m_isShowFlag = true;
    }
    else
    {
      //动画方式隐藏：渐入效果
        hideAnimation(ui->musiclist);
        ui->musiclist->hide();


         m_isShowFlag = false;
    }


}

//播放音乐
void Widget:: starPlayMusic()
{
    QString musicName = ui->musiclist->currentItem()->text();
    //音乐的绝对路径
    QString musicAbsPath = m_musicDir + musicName + ".mp3";
    //加载音乐
    m_player->setMedia(QUrl::fromLocalFile(musicAbsPath));
    //播放音乐
    handlePlaySlot();
}
//处理下一首
void Widget:: handlePrevSlot()
{
    //当前播放的行
    int currentRow = ui->musiclist->currentRow();

    int prevRow = 0;

    if(m_mode == ORDER_MODE)
    {
        prevRow = currentRow-1;
        if(prevRow<0)
        {
            prevRow = ui->musiclist->count()-1;
        }
    }
    else if(m_mode == RANDOM_MODE)
    {
        int cnt = 0;
        do{
            prevRow = rand()%ui->musiclist->count();
        }while((currentRow == prevRow) && (cnt<=3));
    }
    else if(m_mode == CIRCLE_MODE)
    {
        prevRow = currentRow;
    }
    //设置音乐歌曲列表高亮
    ui->musiclist->setCurrentRow(prevRow);
    starPlayMusic();
}
//处理下一首
void Widget:: handleNextSlot()
{
    //当前播放的行
    int currentRow = ui->musiclist->currentRow();
    int nextRow = 0;
    if(m_mode == ORDER_MODE)
    {
        nextRow = (currentRow+1)%ui->musiclist->count();
    }
    else if(m_mode == RANDOM_MODE)
    {
        int cnt = 0;
        do
        {
            nextRow = rand() % ui->musiclist->count();
            cnt++;
        }while((currentRow == nextRow) && (cnt<=3));
    }
    else if(m_mode == CIRCLE_MODE)
    {
        nextRow = currentRow;
    }
    ui->musiclist->setCurrentRow(nextRow);
    starPlayMusic();
}

//处理播放模式
void Widget:: handleModeSlot()
{
    if(m_mode == ORDER_MODE)
    {
        m_mode = RANDOM_MODE;
        ui->modeBtn->setIcon(QIcon(":/Icon/random .png"));
    }
    else if(m_mode == RANDOM_MODE)
    {
        m_mode = CIRCLE_MODE;
        ui->modeBtn->setIcon(QIcon(":/Icon/danqu.png"));
    }
    else if(m_mode == CIRCLE_MODE)
    {
        m_mode = ORDER_MODE;
        ui->modeBtn->setIcon(QIcon(":/Icon/order.png"));
    }
}

    //处理播放暂停
void Widget:: handlePlaySlot()
{
    if(m_player->state() == QMediaPlayer::PlayingState)
    {
        m_player->pause();//打开
        ui->playBtn->setIcon(QIcon(":/Icon/play.png"));

    }
    else{
        m_player->play();//关闭
        ui->playBtn->setIcon(QIcon(":/Icon/stop.png"));

    }
}
//加载指定的文件夹
void Widget:: loadAppointMusic(const QString & filepath)
{
    QDir dir(filepath);
    if(dir.exists() == false)
    {
        QMessageBox::warning(this,"文件夹","文件夹打开失败");
        return;
    }
    //程序执行到这个地方，就说明文件夹是存在的
    QFileInfoList fileList = dir.entryInfoList(QDir::Files);
    for(auto element : fileList)
    {
        //判断后缀是否是mp3列表
        if(element.suffix() == "mp3")
        {
            //添加到音乐列表
            ui->musiclist->addItem(element.baseName());
        }
    }
    //默认选择第一行
    ui->musiclist->setCurrentRow(0);



}

Widget::~Widget()
{
    delete ui;
}

